import axios from 'axios'

import fileDownload from 'js-file-download'


// 公用方法库
// 获取系统默认日期
export function getDate() {
  let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let minute =
    date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let second =
    date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  month >= 1 && month <= 9 ? (month = '0' + month) : ''
  day >= 0 && day <= 9 ? (day = '0' + day) : ''
  let timer = year + '年' + month + '月' + day + '日'
  // var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
  // this.defaultTime = timer
  return timer
}

export function getTime() {
  let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let minute =
    date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let second =
    date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  month >= 1 && month <= 9 ? (month = '0' + month) : ''
  day >= 0 && day <= 9 ? (day = '0' + day) : ''
  // var timer = year + '年' + month + '月' + day + '日';
  let timer =
    year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  // this.defaultTime = timer
  return timer
}

export function formatDate(time = new Date(), str) {
  let date = new Date(time)
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let minute =
    date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let second =
    date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  month >= 1 && month <= 9 ? (month = '0' + month) : ''
  day >= 0 && day <= 9 ? (day = '0' + day) : ''

  var timer
  if (str == 'yyyy-MM-dd HH:mm:ss') {
    timer =
      year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  } else if (str == 'yyyy-MM-dd') {
    timer = year + '-' + month + '-' + day
  } else if (str == 'yyyy年MM月dd') {
    timer = year + '年' + month + '月' + day + '日'
  }
  return timer
}

export function exportMethod(modelNameIndex, data, fileName) {
  axios({
    method: 'post',
    url: `${process.env.VUE_APP_BASE_API}/applet-api/excel/e/${modelNameIndex}`,
    data,
    headers: {
      Authorization: localStorage.getItem('token') || '',
    },
    responseType: 'arraybuffer',
  }).then((res) => {
    fileDownload(res.data, `${fileName}.xls`)
  })
}

export default {
  getDate,
  getTime,
  formatDate,
}
